/*
* Copyright (c) 2003, the JUNG Project and the Regents of the University 
* of California
* All rights reserved.
*
* This software is open-source under the BSD license; see either
* "license.txt" or
* http://jung.sourceforge.net/license.txt for a description.
*/
/*
 * Created on Jun 23, 2003
 */
package graph;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

import ai.graph.MyRenderer;
import ai.text.impl.TextAnalyzerImpl;

import edu.uci.ics.jung.graph.ArchetypeVertex;
import edu.uci.ics.jung.graph.DirectedEdge;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.Vertex;
import edu.uci.ics.jung.graph.decorators.VertexStringer;
import edu.uci.ics.jung.graph.impl.DirectedSparseEdge;
import edu.uci.ics.jung.graph.impl.DirectedSparseGraph;
import edu.uci.ics.jung.graph.impl.DirectedSparseVertex;
import edu.uci.ics.jung.graph.impl.UndirectedSparseGraph;
import edu.uci.ics.jung.io.PajekNetReader;
import edu.uci.ics.jung.utils.UserData;
import edu.uci.ics.jung.visualization.FRLayout;
import edu.uci.ics.jung.visualization.GraphDraw;
import edu.uci.ics.jung.visualization.ISOMLayout;
import edu.uci.ics.jung.visualization.Layout;
import edu.uci.ics.jung.visualization.PluggableRenderer;
import edu.uci.ics.jung.visualization.SpringLayout;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.contrib.KKLayout;

/**
 * Illustrates the simplest possible drawing program.
 * Does the following:
 * <ul>
 * <li/>reads in a graph 
 * <li/>creates a <code>Layout</code> based on that graph
 * <li/>creates a <code>Renderer</code>
 * <li/>creates a <code>VisualizationViewer</code> based on that layout and renderer
 * <li/>adds the <code>VisualizationViewer</code> object to a Swing <code>JFrame</code>'s
 * content pane.
 * </ul>
 * Does no processing, no filtering, and no customization of the rendering.
 * 
 * @author danyelf
 */
public class SimpleGraphDraw {

	public static void main(String[] args) throws IOException {
		JFrame jf = new JFrame();
		//Graph g;// = getGraph();
		/*Graph g = new DirectedSparseGraph();
		Vertex v1 =  g.addVertex(new DirectedSparseVertex());
		Vertex v2 =  g.addVertex(new DirectedSparseVertex());
		Vertex v3 =  g.addVertex(new DirectedSparseVertex());


		DirectedEdge e = (DirectedEdge) g.addEdge(new DirectedSparseEdge(v1, v2));
		g.addEdge(new DirectedSparseEdge(v1, v3));
		final String name_key = "name";
		String current_address_key = "address";
		String current_student_key = "student";
		v1.addUserDatum(name_key, "Carl Jung", UserData.SHARED);
		v2.addUserDatum(name_key, "Sigmund Freud", UserData.SHARED);*/
		
		String text ="A musician is a person who plays or composes music.";
//			"Kurt Donald Cobain, " +
//			"was an American musician, best known for his roles as lead singer," +
//			" guitarist, and songwriter for the Seattle-based rock band Nirvana.";
	
		
		Graph g = new TextAnalyzerImpl().parseText(text);
		
		
		PluggableRenderer renderer = new MyRenderer();

		
        VisualizationViewer vv = new VisualizationViewer(new KKLayout(g), renderer);
        
        jf.getContentPane().add(vv);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.pack();
        jf.setVisible(true);
	}
	
	public static void showGraph(Graph g) throws IOException {
		JFrame jf = new JFrame();
		PluggableRenderer renderer = new MyRenderer();
		
        VisualizationViewer vv = new VisualizationViewer(new KKLayout(g), renderer);
        vv.setDoubleBuffered(false);
        jf.getContentPane().add(vv);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.pack();
        jf.setVisible(true);
        
 
	}
	

	public static void storeJPEG(Graph g) throws IOException {
		int width = 1200;
	    int height = 1000;
		PluggableRenderer renderer = new MyRenderer();		
		
		Layout layout = new KKLayout(g);
	
		
        VisualizationViewer vv = new VisualizationViewer(layout, renderer);
       vv.setSize(width, height);
     
       layout.resize(new Dimension(width, height));
   	while(layout.incrementsAreDone()) {
		layout.advancePositions();
	}
		
	    Color bg = Color.WHITE;

	    BufferedImage bi = new BufferedImage((int)layout.getCurrentSize().getWidth(),(int)layout.getCurrentSize().getHeight(),BufferedImage.TYPE_INT_BGR);
	    Graphics2D graphics = bi.createGraphics();
	    graphics.setColor(bg);
	    graphics.fillRect(0,0, width, height);
	    vv.paint(graphics);

	    try{
	       ImageIO.write(bi,"jpeg",new File("graph.jpg"));
	    }catch(Exception e){e.printStackTrace();}
	    System.exit(0);
	    
	}

	/**
	 * Generates a graph: in this case, reads it from the file
	 * "samples/datasetsgraph/simple.net"
	 * @return A sample undirected graph
	 */
	public static Graph getGraph() throws IOException {
        PajekNetReader pnr = new PajekNetReader();
        Graph g = pnr.load("M:/Projects/java/_libs/jung/src/samples/datasets/simple.net");
		return g;
	}
}